 package com.gmrz.uaf.common.service.challengestore;
 
 import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.crypto.CryptoEngine;
import com.gmrz.uaf.crypto.spi.handlers.HashHandler;
import com.gmrz.util.Convert;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Longs;
import com.google.inject.Singleton;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.security.GeneralSecurityException;
 
 @Singleton
 public class UAFChallengeStoreUtil
 {
   private static final Logger LOG = LogManager.getLogger(UAFChallengeStoreUtil.class);
 
   public static String createChallengeDataKey(byte[] nonce, long timestamp)
     throws GeneralSecurityException
   {
     byte[] ts = Longs.toByteArray(timestamp);
     CryptoEngine cryptoEngine = GuiceUtil.getProcessorInjector().getInstance(CryptoEngine.class);
 
     HashHandler hashHandler = cryptoEngine.getDataHashHandler();
     // Bytes.concat() 将两个字节数组合成一个数组
     byte[] hashKey = hashHandler.hash(Bytes.concat(nonce, ts));

     //int Ranvalue= (int)(1+Math.random()*(50000-1+1));
     String key = Convert.toBase64(hashKey);//+String.valueOf(Ranvalue);
     LOG.debug("Challenge Data Key = {} "+" rk:"+Convert.toBase64(nonce), key);
     return key;
   }
 }
